import { login, getUserInfo, getUserDetailById } from '@/api/user'
// import { setToken as setTokenLocal, getToken as getTokenLocal } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: null,
    userInfo: {},
    timeStamp: ''
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      state.timeStamp = Date.now()
    },
    setUserInfo(state, payload) {
      state.userInfo = payload
    }
  },
  actions: {
    async loginAction(context, payload) {
      const res = await login(payload)
      // console.log(res)
      context.commit('setToken', res)
    },
    async getUserInfo(context) {
      const res = await getUserInfo()
      const detail = await getUserDetailById(res.userId)
      // console.log(detail)
      const userInfo = { ...res, ...detail }
      // console.log(res)
      context.commit('setUserInfo', userInfo)
      return res
    },
    logout(context) {
      context.commit('setToken', null)
      context.commit('setUserInfo', {})
      // 清除路由规则，在子模块中调用另一个子模块中的mutations方法
      context.commit('permission/getRoutes', [], { root: true })
    }
  }
}
